package edu.bw.threadlocal;

public class ThreadLocalDemo {
    private static ThreadLocal<String> threadLocal=new ThreadLocal<>();
    public static void main(String[] args) {
        //创建一个THreadLocal变量,实现每个线程都有其独立的String对象

        Thread thread01 = new Thread(() -> {
            //相当于将"我是线程01的数据"和当前线程绑定
            threadLocal.set("我是线程01的数据");
            System.out.println(Thread.currentThread().getName() + "获取的数据为:" + threadLocal.get());
        }, "Thread-01");


        //创建线程Thread-02,绑定自己的String对象
        Thread thread02 = new Thread(() -> {
            threadLocal.set("我是线程02的数据");
            System.out.println(Thread.currentThread().getName() + "获取的数据为:" + threadLocal.get());
        }, "Thread-02");

        //启动线程
        thread01.start();
        thread02.start();


    }

}
